<?php

namespace App\Http\Service\Impl;

use App\Http\Controllers\BaseController;
use App\Http\Instance\Singleton;
use App\Http\Service\IGoods;
use App\Http\Service\IIndex;
use App\Http\Service\IHome;
use App\User;
use app\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use phpDocumentor\Reflection\Types\Array_;
use Ramsey\Uuid\Uuid;
use Illuminate\Support\Facades\Session;
use Faker\Generator as Faker;
use Hashids\Hashids;
use function foo\func;

/**
 * 前台网站登陆
 * Class HomeImpl
 * @param username               必填,长度不能小于3
 * @param password2              必填,长度大于6小于18
 * @param password_confirmation  必填,且和上一个密码输入一致
 * @param email                  必填,email格式
 * @param validcode              必填,
 * @package App\Http\Service\Impl
 * @author zm
 */
class HomeImpl  implements IHome
{
    public function appreg($request)
    {
        $rules     = [
            "phone"                 => "required|unique:users,phone",
            "email"                 => "required|unique:users,email|email",
            "password"              => "required|min:6|max:18",
            "password_confirmation" => "required|same:password",
        ];
        $message   = [
            'phone.required'                 => '电话号码不能为空',
            'phone.unique'                   => '该号码已经注册过了',
            'username.min'                   => '用户名长度不能小于3个字符',
            'password.required'              => '密码不能为空',
            'password.min'                   => '密码长度不能小于6个字符',
            'password_confirmation.required' => '确认密码不能为空',
            'password_confirmation.same'     => '两次密码输入不一致',
            'password.max'                   => '密码长度不能大于18个字符',
            'email.required'                 => '邮箱不能为空',
            'email.email'                    => '邮箱格式不正确',
            'email.unique'                   => '该邮箱已经注册过了',

        ];
        $validator = Validator::make($request->all(), $rules, $message);
        if ($validator->fails()) {
            $arr  = $validator->errors()->all();
            $type = 1;
        } else {
            $arr              = ['注册成功'];
            $type             = 2;
            $data             = $request->toArray();
            $data['vipstart'] = date('Y1-m-d H:i:s', time());
            $data['vipend']   = date('Y-m-d H:i:s', time());
            $data['password'] = md5($data['password']);
            $info             = User::create($data);
            $id               = $info->id;//获取插入数据之后的主键id
            $hashids          = new Hashids('', 6);
            $info->account    = $hashids->encode($id);//uniqid();$hashids->encode($id);
            $info->id         = $id;
            $info->save();

        }
        return Singleton::getInstance()->returnMsg1($type, $arr);//response()->json($arr);
    }

    /**
     * 用户登陆
     * 202cb962ac59075b964b07152d234b70
     */
    public function applogin($request)
    {

        if (empty($request->phone)) {
            $arr  = ['账号不能为空'];
            $type = 1;
        } else if (empty($request->password)) {
            $arr  = ['密码不能为空'];
            $type = 1;
        }
        $data = [
            "phone"    => $request->phone,
            "password" => md5($request->password)
        ];

        $info = User::where(['phone' => $request->phone, 'password' => md5($request->password)])->first();
        
        if($info){
            //如果有账号密码正确
            $arr  = ['登录成功'];
            $type = 2;
            //缓存用户id
            Session::put("userid",$info->id);
        }else {
            $arr  = ['账号/密码错误'];
            $type = 1;
        }
        return Singleton::getInstance()->returnMsg1($type, $arr);//response()->json($arr);

    }

    public function getGoodlist($where)
    {
        // TODO: Implement getGoodlist() method.
        $new_where = array_merge(["sffb" => 1], $where);
        return DB::table("goodslist")->where($new_where)->get();
    }


    public function getConfig()
    {
        return [
            'app_id'         => config('global.app_id'),//你创建应用的APPID
            'notify_url'     => config('global.notify_url'),//异步回调地址
            'return_url'     => config('global.return_url'),//同步回调地址
            'ali_public_key' => config('global.ali_public_key'),//是支付宝公钥，不是应用公钥,  公钥要写成一行,不要换行
            'private_key'    => config('global.private_key'),//密钥,密钥要写成一行,不要换行
            'log'            => [
                'file'     => './logs/alipay.log',
                'level'    => 'info', // 建议生产环境等级调整为 info，开发环境为 debug
                'type'     => 'single', // optional, 可选 daily.
                'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
            ],
            'http'           => [ // optional
                'timeout'         => 5.0,
                'connect_timeout' => 5.0,
                // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
            ],
            //'mode' => 'dev', // optional,设置此参数，将进入沙箱模式
        ];
    }

    /**
     * 如果查到数据返回true,如果没查到就返回false
     */
    public function ishava_field($db, $field, $value)
    {
        $info = DB::table($db)->where($field, $value)->get()->toArray();
        return count($info) > 0 ? true : false;
    }

}
